package co.fitstart.admin.web.controller.course;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.search.CourseRoundSearch;
import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.CoachService;
import co.fitstart.admin.service.CourseRoundService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/rounds-search")
@MenuSetting("menu-course")
public class RoundSearchController {
    
    @Autowired
    private SiteService siteService;
    
    @Autowired
    private CoachService coachService;
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private PrincipalService principalService;
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-round-search")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView("course/search/round-search-list");
        
        return modelAndView;
    }
    
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-round-search")
    public ModelAndView onSearch(@Secure Login login,
                                 CourseRoundSearch courseRoundSearch,
                                 @RequestParam(value = "siteId", required = false) Long siteId,
                                 @RequestParam(value = "coachId", required = false) Long coachId,
                                 HttpServletRequest request,
                                 Page page) {
        ModelAndView modelAndView = new ModelAndView("course/search/round-search-list");
        
        if(login.hasSite()) {
            courseRoundSearch.setSite(login.getSite());
        } else if(siteId != null) {
            courseRoundSearch.setSite(siteService.load(siteId));
        }
        
        if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            List<Site> sites = siteService.listSite(principal);
            
            courseRoundSearch.setSites(sites);
        }
        
        if(courseRoundSearch.getSites() == null || !courseRoundSearch.getSites().isEmpty()) {
            if(coachId != null) {
                courseRoundSearch.setCoach(coachService.load(coachId));
            }
            
            // list order by page & status
            List<CourseRound> courseRounds = courseRoundService.search(courseRoundSearch, page);
            modelAndView.addObject("courseRounds", courseRounds);
            
            // get total
            int total = courseRoundService.totalCourseRound(courseRoundSearch);
            modelAndView.addObject("total", total);
        } else {
            modelAndView.addObject("total", 0);
        }
        
        // add query string
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
    }
}
